﻿using AspDotNet.Core6.CustomTagHelper.Models;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Text;

namespace AspDotNet.Core6.CustomTagHelper.CustomTagHelpers
{
    [HtmlTargetElement("ctable")]
    public class ShowPerson : TagHelper
    {
        public IEnumerable<Person> People { get; set; }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "table";
            output.TagMode = TagMode.StartTagAndEndTag;

            output.Attributes.SetAttribute("class", "table table-sm table-bordered");
            StringBuilder builder = new StringBuilder();

            foreach (var item in People)
            {
                builder.AppendFormat("<tr>").AppendLine();
                builder.AppendFormat("  <td>{0}</td>", item.Name).AppendLine();
                builder.AppendFormat("  <td>{0}</td>", item.Age).AppendLine();
                builder.AppendFormat("  <td>{0}</td>", item.Address).AppendLine();
                builder.AppendFormat("</tr>").AppendLine();
            }

            output.Content.SetHtmlContent(builder.ToString());
        }
    }
}
